﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using fideeraSoft.Utils;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;

namespace fideeraSoft.Paneles
{
    public class ConsultaProduccionesIniciadasPanel:PanelGeneral
    {
        private System.Windows.Forms.DataGridView dgvMostrar;
        private System.Windows.Forms.TableLayoutPanel tlpBuscarPorId;
        private System.Windows.Forms.TextBox tbBuscarPorId;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.DataGridView dgvMostrar2;
        private System.Windows.Forms.Button btBuscarPorId;

        private SqlDataAdapter adapter;
        private DateTimePicker dtpBuscarFin;
        private Button btBuscarEntreFecha;
        private DateTimePicker dtpBuscarIni;
        private TableLayoutPanel tlpBuscarEntreFechas;

        private DataSet ds;

        private int locationBuscarLoteId=0;
        private int locationBuscarEntreFechas = 0;
        private TableLayoutPanel tlpExportExcel;
        private Button btExportExcel2;
        private TableLayoutPanel tlpExportExcel2;
        private Button btExportExcel;
        int inPosicionDgvMatPrima = 0;
        int locationTablaExportExcel2=0;
        int locationDataGridView = 0;


        public ConsultaProduccionesIniciadasPanel(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            try
            {
                InitializeComponent();
                this.formatearTableTitulo("CONSULTA DE PRODUCCIONES INICIADAS");

                string query = "SELECT pr.ID AS [ID Producción], p.nombre AS Producto,pr.fechaInicio AS [Fecha de Inicio], pr.observacion AS Observación FROM Produccion AS pr INNER JOIN Producto AS p ON pr.producto=p.ID AND pr.fechaInicio IS NOT NULL AND pr.fechaFin IS NULL";
                adapter = SQLclass.obtenerAdapter(query);
                if (adapter != null)
                {
                    ds = new DataSet();
                    adapter.Fill(ds);




                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }else
                    {
                        dgvMostrar.DataSource = ds.Tables[0];

                        if (dgvMostrar.CurrentCell != null)
                        {
                            cargarProduccionesXMatPrimaEnDataGridView(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
                        }
                        else 
                        {
                            
                        }
                    }

                    formatearTablaExportExcel();

                    formatearDataGrid();

                    formatearTablaBuscarProduccionId();

                    formatearTablaBuscarEntreFechas();

                    formatearDataGrid2();

                    formatearTablaExportExcel2();

                    formatearTablaBotones();


                    this.Controls.Add(tlpExportExcel);

                    this.Controls.Add(dgvMostrar);
                    
                    this.Controls.Add(tlpBuscarPorId);

                    this.Controls.Add(tlpBuscarEntreFechas);

                    this.Controls.Add(dgvMostrar2);
                    
                    this.Controls.Add(tlpExportExcel2);

                    this.Controls.Add(tlpBotones);


                }

                else
                {

                    MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.handler.backToPrincipal();
                }

            }
            catch (MiException ex)
            {

                CLogger.Append("[ConsultaProduccionesIniciadasPanel] Exception: " + ex, CLogger.ERROR);      //DEBUG

            }
        }

        #region FORMATEO

        private void formatearTablaExportExcel()
        {
            tlpExportExcel.Controls.Add(btExportExcel);

            // int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, tlpExportExcel.Size.Width);

            tlpExportExcel.Location = new System.Drawing.Point(50, this.InitialRealPointVertical);
        }

        private void formatearDataGrid()
        {
            locationDataGridView = this.InitialRealPointVertical + tlpExportExcel.Size.Height + 10;

            dgvMostrar.AutoGenerateColumns = true;
            dgvMostrar.Size = new Size(this.Size.Width - 100, 300);
            dgvMostrar.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMostrar.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMostrar.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dgvMostrar.BorderStyle = BorderStyle.Fixed3D;

            dgvMostrar.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMostrar.Size.Width), locationDataGridView);
        }

        private void formatearTablaBuscarProduccionId()
        {
            tlpBuscarPorId.Controls.Add(btBuscarPorId);
            tlpBuscarPorId.Controls.Add(tbBuscarPorId);

            //tlpBuscarProv.Size = new Size(lbBuscarProvCuil.Size.Width + tbBuscarProvCuil.Size.Width + 10,tbBuscarProvCuil.Size.Height);
            locationBuscarLoteId = locationDataGridView + dgvMostrar.Size.Height + 15;

            int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, tlpBuscarPorId.Size.Width);            

            tlpBuscarPorId.Location = new Point(wid, locationBuscarLoteId);


        }


        private void formatearTablaBuscarEntreFechas()
        {
            //tlpBuscarEntreFechas.Size = new Size(400, tbBuscarRemitoCod.Size.Height + 9);
            locationBuscarEntreFechas = locationBuscarLoteId + tlpBuscarPorId.Size.Height + 5;

            tlpBuscarEntreFechas.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, tlpBuscarEntreFechas.Size.Width), locationBuscarEntreFechas);

            tlpBuscarEntreFechas.Controls.Add(btBuscarEntreFecha);

            dtpBuscarIni.Value = System.DateTime.Now;
            dtpBuscarFin.Value = System.DateTime.Now;

            tlpBuscarEntreFechas.Controls.Add(dtpBuscarIni);
            tlpBuscarEntreFechas.Controls.Add(dtpBuscarFin);


        }

        private void formatearDataGrid2()
        {
            

            inPosicionDgvMatPrima = locationBuscarEntreFechas + tlpBuscarEntreFechas.Size.Height + 15;

            dgvMostrar2.AutoGenerateColumns = true;
            dgvMostrar2.Size = new Size(this.Size.Width - 100, 160);
            dgvMostrar2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvMostrar2.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvMostrar2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dgvMostrar2.BorderStyle = BorderStyle.Fixed3D;

            dgvMostrar2.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvMostrar2.Size.Width), this.inPosicionDgvMatPrima);
        }


        private void formatearTablaExportExcel2()
        {
            tlpExportExcel2.Controls.Add(btExportExcel2);

            locationTablaExportExcel2 = inPosicionDgvMatPrima + dgvMostrar2.Size.Height + 10;

            //int wid = MetodosGenerales.calcularCenterWidth(handler.FormFrame.Size.Width, tlpExportExcel2.Size.Width);

            tlpExportExcel2.Location = new System.Drawing.Point(50, locationTablaExportExcel2);
        }

        private void formatearTablaBotones()
        {
            int locationBtCambios = locationTablaExportExcel2 + tlpExportExcel2.Size.Height + 15;
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.handler.FormFrame.Size.Width, tlpBotones.Size.Width), locationBtCambios);

            tlpBotones.Controls.Add(btVolver);


        }


        #endregion

        #region INICIALIZAR COMPONETES
        private void InitializeComponent()
        {
            this.dgvMostrar = new System.Windows.Forms.DataGridView();
            this.tlpBuscarPorId = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarPorId = new System.Windows.Forms.TextBox();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.btBuscarPorId = new System.Windows.Forms.Button();
            this.dgvMostrar2 = new System.Windows.Forms.DataGridView();
            this.dtpBuscarFin = new System.Windows.Forms.DateTimePicker();
            this.btBuscarEntreFecha = new System.Windows.Forms.Button();
            this.dtpBuscarIni = new System.Windows.Forms.DateTimePicker();
            this.tlpBuscarEntreFechas = new System.Windows.Forms.TableLayoutPanel();
            this.tlpExportExcel = new System.Windows.Forms.TableLayoutPanel();
            this.btExportExcel2 = new System.Windows.Forms.Button();
            this.tlpExportExcel2 = new System.Windows.Forms.TableLayoutPanel();
            this.btExportExcel = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar2)).BeginInit();
            this.SuspendLayout();
            // 
            // dgvMostrar
            // 
            this.dgvMostrar.AllowUserToResizeRows = false;
            this.dgvMostrar.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMostrar.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
            this.dgvMostrar.Location = new System.Drawing.Point(0, 0);
            this.dgvMostrar.Name = "dgvMostrar";
            this.dgvMostrar.ReadOnly = true;
            this.dgvMostrar.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvMostrar.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dgvMostrar.Size = new System.Drawing.Size(240, 150);
            this.dgvMostrar.TabIndex = 0;
            this.dgvMostrar.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrar_CellClick);
            this.dgvMostrar.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarLotes_CellContentClick);
            // 
            // tlpBuscarPorId
            // 
            this.tlpBuscarPorId.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarPorId.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarPorId.ColumnCount = 2;
            this.tlpBuscarPorId.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarPorId.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarPorId.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarPorId.Name = "tlpBuscarPorId";
            this.tlpBuscarPorId.RowCount = 1;
            this.tlpBuscarPorId.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarPorId.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarPorId.Size = new System.Drawing.Size(400, 40);
            this.tlpBuscarPorId.TabIndex = 0;
            // 
            // tbBuscarPorId
            // 
            this.tbBuscarPorId.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.tbBuscarPorId.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarPorId.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarPorId.MaxLength = 8;
            this.tbBuscarPorId.Name = "tbBuscarPorId";
            this.tbBuscarPorId.Size = new System.Drawing.Size(100, 30);
            this.tbBuscarPorId.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 1;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(100, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // btBuscarPorId
            // 
            this.btBuscarPorId.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btBuscarPorId.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarPorId.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarPorId.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarPorId.Location = new System.Drawing.Point(0, 0);
            this.btBuscarPorId.Name = "btBuscarPorId";
            this.btBuscarPorId.Size = new System.Drawing.Size(75, 23);
            this.btBuscarPorId.TabIndex = 0;
            this.btBuscarPorId.Text = "Buscar Producción (ID)";
            this.btBuscarPorId.UseVisualStyleBackColor = false;
            this.btBuscarPorId.Click += new System.EventHandler(this.btBuscarPorId_Click);
            // 
            // dgvMostrar2
            // 
            this.dgvMostrar2.AllowUserToResizeRows = false;
            this.dgvMostrar2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvMostrar2.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnF2;
            this.dgvMostrar2.Location = new System.Drawing.Point(0, 0);
            this.dgvMostrar2.Name = "dgvMostrar2";
            this.dgvMostrar2.ReadOnly = true;
            this.dgvMostrar2.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvMostrar2.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dgvMostrar2.Size = new System.Drawing.Size(240, 150);
            this.dgvMostrar2.TabIndex = 0;
            this.dgvMostrar2.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarMatPrima_CellClick);
            this.dgvMostrar2.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMostrarMatPrima_CellContentClick);
            // 
            // dtpBuscarFin
            // 
            this.dtpBuscarFin.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarFin.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarFin.Name = "dtpBuscarFin";
            this.dtpBuscarFin.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarFin.TabIndex = 0;
            // 
            // btBuscarEntreFecha
            // 
            this.btBuscarEntreFecha.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarEntreFecha.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarEntreFecha.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarEntreFecha.Location = new System.Drawing.Point(0, 0);
            this.btBuscarEntreFecha.Name = "btBuscarEntreFecha";
            this.btBuscarEntreFecha.Size = new System.Drawing.Size(75, 23);
            this.btBuscarEntreFecha.TabIndex = 0;
            this.btBuscarEntreFecha.Text = "Buscar entre fechas";
            this.btBuscarEntreFecha.UseVisualStyleBackColor = false;
            this.btBuscarEntreFecha.Click += new System.EventHandler(this.btBuscarEntreFecha_Click);
            // 
            // dtpBuscarIni
            // 
            this.dtpBuscarIni.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarIni.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarIni.Name = "dtpBuscarIni";
            this.dtpBuscarIni.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarIni.TabIndex = 0;
            // 
            // tlpBuscarEntreFechas
            // 
            this.tlpBuscarEntreFechas.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarEntreFechas.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarEntreFechas.ColumnCount = 3;
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarEntreFechas.Name = "tlpBuscarEntreFechas";
            this.tlpBuscarEntreFechas.RowCount = 1;
            this.tlpBuscarEntreFechas.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarEntreFechas.Size = new System.Drawing.Size(600, 40);
            this.tlpBuscarEntreFechas.TabIndex = 0;
            // 
            // tlpExportExcel
            // 
            this.tlpExportExcel.BackColor = System.Drawing.Color.Transparent;
            this.tlpExportExcel.ColumnCount = 1;
            this.tlpExportExcel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel.Location = new System.Drawing.Point(0, 0);
            this.tlpExportExcel.Name = "tlpExportExcel";
            this.tlpExportExcel.RowCount = 1;
            this.tlpExportExcel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel.Size = new System.Drawing.Size(100, 35);
            this.tlpExportExcel.TabIndex = 0;
            // 
            // btExportExcel2
            // 
            this.btExportExcel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btExportExcel2.BackColor = System.Drawing.Color.AliceBlue;
            this.btExportExcel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btExportExcel2.Font = new System.Drawing.Font("Bodoni MT", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btExportExcel2.Location = new System.Drawing.Point(0, 0);
            this.btExportExcel2.Name = "btExportExcel2";
            this.btExportExcel2.Size = new System.Drawing.Size(75, 23);
            this.btExportExcel2.TabIndex = 0;
            this.btExportExcel2.Text = "Exportar";
            this.btExportExcel2.UseVisualStyleBackColor = false;
            this.btExportExcel2.Click += new System.EventHandler(this.btExportExcel2_Click);
            // 
            // tlpExportExcel2
            // 
            this.tlpExportExcel2.BackColor = System.Drawing.Color.Transparent;
            this.tlpExportExcel2.ColumnCount = 1;
            this.tlpExportExcel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel2.Location = new System.Drawing.Point(0, 0);
            this.tlpExportExcel2.Name = "tlpExportExcel2";
            this.tlpExportExcel2.RowCount = 1;
            this.tlpExportExcel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpExportExcel2.Size = new System.Drawing.Size(100, 35);
            this.tlpExportExcel2.TabIndex = 0;
            // 
            // btExportExcel
            // 
            this.btExportExcel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.btExportExcel.BackColor = System.Drawing.Color.AliceBlue;
            this.btExportExcel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btExportExcel.Font = new System.Drawing.Font("Bodoni MT", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btExportExcel.Location = new System.Drawing.Point(0, 0);
            this.btExportExcel.Name = "btExportExcel";
            this.btExportExcel.Size = new System.Drawing.Size(75, 23);
            this.btExportExcel.TabIndex = 0;
            this.btExportExcel.Text = "Exportar";
            this.btExportExcel.UseVisualStyleBackColor = false;
            this.btExportExcel.Click += new System.EventHandler(this.btExportExcel_Click);
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgvMostrar2)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion 

        private void cargarProduccionesXMatPrimaEnDataGridView(string idProduccion)
        {
            try
            {
                if (dgvMostrar.CurrentCell == null)
                {
                    return;
                }

                dgvMostrar2.DataSource = null;
                dgvMostrar2.Rows.Clear();

                string query = "SELECT mp.ID AS [ID Materia Prima], mp.nombre AS [Nombre de Materia Prima], mp.codigo AS Código, prm.cantidad AS Cantidad, prm.unidad AS Unidad FROM MateriaPrima AS mp INNER JOIN Produccion_X_MateriaPrima AS prm ON  mp.ID=prm.materiaPrima AND prm.produccion='"+idProduccion+"'";
                
                adapter = SQLclass.obtenerAdapter(query);
                if (adapter != null)
                {
                    ds = new DataSet();
                    adapter.Fill(ds);

                    if (ds.Tables[0].Rows.Count <= 0)
                    {
                        MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADA PARA ESA PRODUCCIÓN", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else
                    {
                        dgvMostrar2.DataSource = ds.Tables[0];
                    }
                }

                else
                {

                    MessageBox.Show("NO HAY NINGUNA MATERIA PRIMA CARGADA PARA ESA PRODUCCIÓN", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    //this.handler.backToPrincipal();
                }

            }
            catch (MiException ex)
            {

                CLogger.Append("[cargarProduccionesXMatPrimaEnDataGridView] Exception: " + ex, CLogger.ERROR);      //DEBUG

            }
        }

        #region BOTONES
        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btBuscarPorId_Click(object sender, EventArgs e)
        {
            if (dgvMostrar.Rows.Count <= 0)
                return;

            dgvMostrar.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarPorId.Text))
            {
                string searchValue = tbBuscarPorId.Text;
                try
                {
                    foreach (DataGridViewRow row in dgvMostrar.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(searchValue))
                        {
                            dgvMostrar.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            tbBuscarPorId.Clear();

                            dgvMostrar.CurrentCell = row.Cells[0];
                            //dgvMostrarLotes.FirstDisplayedScrollingRowIndex=row.Index;

                            if (dgvMostrar.CurrentCell != null)
                            {
                                cargarProduccionesXMatPrimaEnDataGridView(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
                            }
                            break;

                        }

                    }

                }
                catch (Exception exc)
                {
                    tbBuscarPorId.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA PRODUCCIÓN BUSCADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO PRODUCCIÓN A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void btBuscarEntreFecha_Click(object sender, EventArgs e)
        {
            if (dgvMostrar.CurrentCell == null)
            {
                MessageBox.Show("NO HAY NINGUN VALOR CARGADO EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            dgvMostrar.ClearSelection();
            if ((dtpBuscarIni.Value == null) || (dtpBuscarFin.Value == null))
            {
                MessageBox.Show("VALORES DE FECHA ERRONEOS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //if (dtpBuscarIni.Value > dtpBuscarFin.Value) 
            if (DateTime.Compare(dtpBuscarIni.Value, dtpBuscarFin.Value) > 0)
            {
                MessageBox.Show("LA FECHA PRIMARIA NO PUEDE SER MAYOR QUE LA FECHA SECUNDARIA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //MessageBox.Show("FECHA 1: " + dtBuscarIni.Value.ToString("MM/dd/yyyy") + " FECHA 2: " + dtBuscarFin.Value.ToString("MM/dd/yyyy"), "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            string query = "SELECT pr.ID AS [ID Producción], p.nombre AS Producto,pr.fechaInicio AS [Fecha de Inicio], pr.observacion AS Observación FROM Produccion AS pr INNER JOIN Producto AS p ON pr.producto=p.ID AND pr.fechaInicio IS NOT NULL AND pr.fechaFin IS NULL AND (fechaInicio BETWEEN @FECHAINICIO AND @FECHAFIN)";
                        
            adapter = SQLclass.obtenerAdapter(query);

            adapter.SelectCommand.Parameters.AddWithValue("@FECHAINICIO", dtpBuscarIni.Value);
            //adapter.SelectCommand.Parameters.AddWithValue("@FECHAFIN", dtpBuscarFin.Value.ToString("MM/dd/yyyy") + " 23:59");

            adapter.SelectCommand.Parameters.AddWithValue("@FECHAFIN", dtpBuscarFin.Value);

            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);

                if (ds.Tables[0].Rows.Count <= 0)
                {
                    MessageBox.Show("NO SE ENCONTRARON PRODUCCIONES ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
                else
                {
                    dgvMostrar2.DataSource = "";
                    dgvMostrar2.Refresh();

                    dgvMostrar.DataSource = ds.Tables[0];

                }


            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("ERROR. NO SE ENCONTRARON PRODUCCIONES ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }
        }

        private void dgvMostrar_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;

            if (dgvMostrar.CurrentCell != null)
            {
                cargarProduccionesXMatPrimaEnDataGridView(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
            }
        }

        private void dgvMostrarLotes_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;

            if (dgvMostrar.CurrentCell != null)
                cargarProduccionesXMatPrimaEnDataGridView(dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString());
        }

        private void dgvMostrarMatPrima_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar2.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void dgvMostrarMatPrima_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvMostrar2.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }
        
        #endregion

        #region EXCEL
        private void btExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvMostrar.CurrentCell == null)
                {
                    MessageBox.Show("NO HAY DATOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                dgvMostrar.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dgvMostrar.CurrentCell = dgvMostrar.Rows[dgvMostrar.CurrentCell.RowIndex].Cells[0];


                copyAlltoClipboard();
                Microsoft.Office.Interop.Excel.Application xlexcel;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

                object misValue = System.Reflection.Missing.Value;
                xlexcel = new Microsoft.Office.Interop.Excel.Application();

                xlexcel.Visible = true;

                xlWorkBook = xlexcel.Workbooks.Add(misValue);

                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Name = "Producciones Iniciadas";

                Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1];
                CR.Select();
                xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);

                formatearExcel1(xlWorkSheet);


            }
            catch (Exception ex)
            {
                CLogger.Append("[btExportExcel_Click] Exception  export: " + ex, CLogger.INFO);      //DEBUG
            }
        }
        
        private static void formatearExcel1(Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet)
        {
            

            xlWorkSheet.Cells[1, 1] = "ID Producción";
            xlWorkSheet.Cells[1, 2] = "Producto";
            xlWorkSheet.Cells[1, 3] = "Fecha Inicio Producción";            
            xlWorkSheet.Cells[1, 4] = "Observación";

            xlWorkSheet.Cells[1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 1].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 2].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 3].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 4].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 4].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
            
            xlWorkSheet.Columns.AutoFit();
        }

        private void copyAlltoClipboard()
        {
            dgvMostrar.SelectAll();
            DataObject dataObj = dgvMostrar.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

        private void btExportExcel2_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvMostrar2.CurrentCell == null)
                {
                    MessageBox.Show("NO HAY DATOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                dgvMostrar2.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dgvMostrar2.CurrentCell = dgvMostrar2.Rows[dgvMostrar2.CurrentCell.RowIndex].Cells[0];


                copyAlltoClipboard2();
                Microsoft.Office.Interop.Excel.Application xlexcel;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

                object misValue = System.Reflection.Missing.Value;
                xlexcel = new Microsoft.Office.Interop.Excel.Application();

                xlexcel.Visible = true;

                xlWorkBook = xlexcel.Workbooks.Add(misValue);

                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Name = "MateriaPrimaPorProducción";

                Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 2];
                CR.Select();
                xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);

                formatearExcel2(xlWorkSheet);

            }
            catch (Exception ex)
            {
                CLogger.Append("[btExportExcel2_Click] Exception  export: " + ex, CLogger.INFO);      //DEBUG
            }
        }

        private void formatearExcel2(Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet)
        {            
            xlWorkSheet.Cells[1, 2] = "Id Materia Prima";
            xlWorkSheet.Cells[1, 3] = "Nombre";
            xlWorkSheet.Cells[1, 4] = "Código";
            xlWorkSheet.Cells[1, 5] = "Cantidad";
            xlWorkSheet.Cells[1, 6] = "Unidad";

            xlWorkSheet.Cells[1, 1] = "Id Producción";

            xlWorkSheet.Cells[2, 1] = dgvMostrar[0, dgvMostrar.CurrentCell.RowIndex].Value.ToString();

            xlWorkSheet.Cells[1, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 1].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 2].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 3].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 4].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 4].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 5].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 5].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Cells[1, 6].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
            xlWorkSheet.Cells[1, 6].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);

            xlWorkSheet.Columns.AutoFit();
        }

        private void copyAlltoClipboard2()
        {
            dgvMostrar2.SelectAll();
            DataObject dataObj = dgvMostrar2.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

        #endregion

    }
}
